package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        // 1. 设置分页参数
        PageHelper.startPage(pageNum,pageSize);

        // 2. 查询数据
      Page<NursingProject> page= nursingProjectMapper.selectByPage(name,status);

        //3. 数据封装
       return PageResponse.of(page,NursingProjectVo.class);

    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        // 1. 把DTO对象抓换成 实体类对象
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        // 2.调用mapper 保存数据
        nursingProjectMapper.insert(bean);

    }

    @Override
    public NursingProjectVo getById(Long id) {

        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtils.copyProperties(nursingProject,nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {

        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);

        nursingProjectMapper.update(nursingProject);
    }
}
